import { createRouter, createWebHashHistory } from 'vue-router'

const Home = () => import('@/views/home/Home.vue')
const Home1 = () => import('@/views/home/c-page/home1/Home1.vue')
const Home2 = () => import('@/views/home/c-page/home2/Home2.vue')
const Home3 = () => import('@/views/home/c-page/home3/Home3.vue')
const About = () => import('@/views/about/About.vue')

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {
      title: '首页',
    },
    children: [
      {
        path: '/home',
        redirect: '/home/home1',
      },
      {
        path: 'home1',
        name: 'home1',
        component: Home1,
        meta: {
          title: '首页-1',
        },
      },
      {
        path: 'home2',
        name: 'home2',
        component: Home2,
        meta: {
          title: '首页-2',
        },
      },
      {
        path: 'home3',
        name: 'home3',
        component: Home3,
        meta: {
          title: '首页-3',
        },
      },
    ],
  },
  {
    path: '/about',
    name: 'about',
    component: About,
    meta: {
      title: '关于',
    },
  },
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {
        top: 0,
      }
    }
  },
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
